Spring Boot এবং Spring Security Integration

Java Technologies - স্প্রিং সিকিউরিটি (Spring Security) - Spring Security এর পরিচিতি
242

Spring Boot এর সঙ্গে Spring Security সহজে ইন্টিগ্রেট করা যায়। Spring Boot স্টার্টার ডিপেনডেন্সি এবং ডিফল্ট কনফিগারেশনের মাধ্যমে এটি আরও সরল হয়েছে। Spring Security অ্যাপ্লিকেশনের জন্য Authentication, Authorization এবং অন্যান্য নিরাপত্তা ফিচার প্রদান করে।


Spring Boot-এ Spring Security ইন্টিগ্রেশন

Step 1: Maven ডিপেনডেন্সি যুক্ত করা

Spring Boot-এ Spring Security যোগ করার জন্য নিচের ডিপেনডেন্সি pom.xml এ যোগ করুন:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

ডিপেনডেন্সি যোগ করার পর, Spring Boot স্বয়ংক্রিয়ভাবে Spring Security কনফিগার করে দেয়।


Step 2: ডিফল্ট Spring Security

Spring Boot ডিফল্টভাবে:

  1. সমস্ত এন্ডপয়েন্ট সুরক্ষিত রাখে।
  2. /login নামে একটি ডিফল্ট লগইন পেজ প্রদান করে।
  3. একটি ডিফল্ট ইউজারনেম (user) এবং অটো-জেনারেটেড পাসওয়ার্ড ব্যবহার করে।

ডিফল্ট লগইন পেজে অ্যাক্সেস করতে:

http://localhost:8080/login

অ্যাপ্লিকেশন স্টার্ট করার সময় কনসোলে জেনারেট হওয়া পাসওয়ার্ড দেখতে পাবেন।


Step 3: Custom Security Configuration

Spring Boot-এ ডিফল্ট কনফিগারেশন কাস্টমাইজ করতে আপনাকে @EnableWebSecurity এবং WebSecurityConfigurerAdapter ব্যবহার করতে হবে।

Custom Security Configuration উদাহরণ:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/public/**").permitAll() // উন্মুক্ত এন্ডপয়েন্ট
                .antMatchers("/admin/**").hasRole("ADMIN") // ADMIN রোলের জন্য
                .antMatchers("/user/**").hasRole("USER") // USER রোলের জন্য
                .anyRequest().authenticated() // বাকি সব রিকোয়েস্ট অথেন্টিকেশন চায়
            .and()
            .formLogin()
                .loginPage("/custom-login") // কাস্টম লগইন পেজ
                .permitAll()
            .and()
            .logout()
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("admin").password(passwordEncoder().encode("admin")).roles("ADMIN")
            .and()
                .withUser("user").password(passwordEncoder().encode("user")).roles("USER");
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

Step 4: কাস্টম লগইন পেজ তৈরি করা

Spring Security-এর ডিফল্ট লগইন পেজ পরিবর্তন করে একটি কাস্টম পেজ তৈরি করতে পারবেন।

Controller:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class LoginController {

    @GetMapping("/custom-login")
    public String login() {
        return "login";
    }
}
HTML (login.html):
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Custom Login</title>
</head>
<body>
    <h1>Login</h1>
    <form th:action="@{/login}" method="post">
        <label for="username">Username:</label>
        <input type="text" id="username" name="username"><br>
        <label for="password">Password:</label>
        <input type="password" id="password" name="password"><br>
        <button type="submit">Login</button>
    </form>
</body>
</html>

Step 5: Database-Based Authentication

Spring Security ইন-মেমরি অথেন্টিকেশন ছাড়াও ডেটাবেস ভিত্তিক অথেন্টিকেশন সমর্থন করে।

Database Configuration উদাহরণ:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class DatabaseSecurityConfig extends WebSecurityConfigurerAdapter {

    private final DataSource dataSource;

    public DatabaseSecurityConfig(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .jdbcAuthentication()
            .dataSource(dataSource)
            .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username = ?")
            .authoritiesByUsernameQuery("SELECT username, role FROM authorities WHERE username = ?");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated()
            .and()
            .formLogin()
                .permitAll()
            .and()
            .logout()
                .permitAll();
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
Database Tables:
CREATE TABLE users (
    username VARCHAR(50) PRIMARY KEY,
    password VARCHAR(100) NOT NULL,
    enabled BOOLEAN NOT NULL
);

CREATE TABLE authorities (
    username VARCHAR(50) NOT NULL,
    role VARCHAR(50) NOT NULL,
    FOREIGN KEY (username) REFERENCES users(username)
);

Spring Security এবং REST API

Spring Security REST API-এর জন্য সহজে ব্যবহারযোগ্য। সাধারণত REST API-এর জন্য JWT (JSON Web Token) ব্যবহার করা হয়।

JWT Token-Based Authentication:
  • ব্যবহারকারী লগইন করলে একটি টোকেন প্রদান করা হয়।
  • ক্লায়েন্ট প্রত্যেক রিকোয়েস্টে সেই টোকেন ব্যবহার করে অথেন্টিকেশন করে।

Spring Security Integration এর সুবিধা

  1. সহজ এবং দ্রুত সেটআপ: Spring Boot-এর স্টার্টার ডিপেনডেন্সি।
  2. Customizable: সম্পূর্ণ কাস্টমাইজেশন সুবিধা।
  3. Secure by Default: Spring Security ডিফল্টভাবে সুরক্ষা নিশ্চিত করে।
  4. Integration: Spring Data, Spring MVC, এবং অন্যান্য Spring মডিউলের সঙ্গে সহজে ইন্টিগ্রেট হয়।

উপসংহার

Spring Boot এবং Spring Security-এর ইন্টিগ্রেশন খুবই শক্তিশালী এবং ব্যবহারবান্ধব। ডিফল্ট সেটআপ দ্রুত সুরক্ষা প্রদান করে, এবং কাস্টমাইজেশন করার মাধ্যমে অ্যাপ্লিকেশন-specific সিকিউরিটি নিশ্চিত করা যায়। এটি ওয়েব অ্যাপ্লিকেশন, REST API, এবং মাইক্রোসার্ভিস আর্কিটেকচারের জন্য আদর্শ।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...